package aof.helper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDBHelper extends SQLiteOpenHelper {

	private static MyDBHelper _dbHelper;
	private static String DB_NAME = "Cost.db";
	private SQLiteDatabase _db;
	private final Context _context;
	private String DB_PATH;
	private String _searchToken;
	private Cursor _cursor;

	// private Vector<Object> _searchResultVec;

	public MyDBHelper(Context context) {
		super(context, DB_NAME, null, 1);
		this._context = context;
		DB_PATH = String.format("/data/data/%s/databases/", context.getPackageName());
		// _searchResultVec = new Vector<Object>();
		_searchToken = "";
	}

	public static MyDBHelper getInstance(Context context) {
		if (_dbHelper == null) {
			_dbHelper = new MyDBHelper(context);
		}
		return _dbHelper;
	}

	public String getLastSearchToken() {
		return _searchToken;
	}

	private boolean checkDataBase() {
		try {
			String myPath = DB_PATH + DB_NAME;
			File f = new File(myPath);
			if (f.exists())
				return true;
			else
				return false;
		} catch (SQLiteException e) {
			e.printStackTrace();
			return false;
		}
	}

	protected void copyDataBase() throws IOException {
		InputStream myInput = _context.getAssets().open(DB_NAME);
		String outFileName = DB_PATH + DB_NAME;
		OutputStream myOutput = new FileOutputStream(outFileName);
		byte[] buffer = new byte[1024];
		int length;
		while ((length = myInput.read(buffer)) > 0) {
			myOutput.write(buffer, 0, length);
		}

		myOutput.flush();
		myOutput.close();
		myInput.close();
	}

	protected void DeleteDataBase() throws IOException {
		File f = new File(DB_PATH + DB_NAME);
		if (f.exists()) {
			f.delete();
		}
	}

	protected void createDataBase() throws IOException {
		boolean dbExist = checkDataBase();
		if (dbExist) {

		} else {
			this.getReadableDatabase();
			try {
				copyDataBase();
			} catch (IOException e) {
				throw new Error("Error copying database");
			}
		}
	}

	public void resetDataBase() {
		try {
			DeleteDataBase();
			createDataBase();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	protected void openDataBase() throws SQLException {
		String myPath = DB_PATH + DB_NAME;
		_db = SQLiteDatabase.openDatabase(myPath, null,
				SQLiteDatabase.OPEN_READWRITE);
	}

	protected void closeDataBase() {
		if (_cursor != null || !_cursor.isClosed()) {
			_cursor.close();
		}
		if (_db != null) {
			_db.close();
			super.close();
		}
	}

	@Override
	public synchronized void close() {
		if (_cursor != null || !_cursor.isClosed()) {
			_cursor.close();
		}
		if (_db != null) {
			_db.close();
			super.close();
		}
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

	protected Boolean Query(String sql) {
		// TODO Auto-generated method stub
		_cursor = _db.rawQuery(sql, null);
		if (_cursor == null)
			return false;
		else {
			_cursor.moveToFirst();
			return true;
		}
	}

	protected Boolean IsReadData() {
		if (_cursor != null && !_cursor.isClosed()) {
			if (_cursor.isLast()) {
				_cursor.close();
				return false;
			} else {
				_cursor.moveToNext();
				return true;
			}
		} else {
			return false;
		}
	}

	// get data on cursor

	protected String getDataString(String columnName) {
		if (_cursor != null && !_cursor.isClosed()) {
			int index = _cursor.getColumnIndex(columnName);
			if (index > -1)
				return _cursor.getString(index);
			else
				return "";
		} else
			return "";
	}

	protected String getDataString(int index) {
		if (_cursor != null && !_cursor.isClosed())
			return _cursor.getString(index);
		else
			return "";
	}

	protected Double getDataDouble(String columnName) {
		if (_cursor != null && !_cursor.isClosed()) {
			int index = _cursor.getColumnIndex(columnName);
			if (index > -1)
				return _cursor.getDouble(index);
			else
				return null;
		} else
			return null;
	}

	protected Double getDataDouble(int index) {
		if (_cursor != null && !_cursor.isClosed())
			return _cursor.getDouble(index);
		else
			return null;
	}

	protected int getDataInt(String columnName) {
		if (_cursor != null && !_cursor.isClosed()) {
			int index = _cursor.getColumnIndex(columnName);
			if (index > -1)
				return _cursor.getInt(index);
			else
				return (Integer) null;
		} else
			return (Integer) null;
	}

	protected int getDataInt(int index) {
		if (_cursor != null && !_cursor.isClosed())
			return _cursor.getInt(index);
		else
			return 0;
	}

	protected byte[] getDataBlob(String columnName) {
		if (_cursor != null && !_cursor.isClosed()) {
			int index = _cursor.getColumnIndex(columnName);
			if (index > -1)
				return _cursor.getBlob(index);
			else
				return null;
		} else
			return null;
	}

	protected byte[] getDataBlob(int index) {
		if (_cursor != null && !_cursor.isClosed())
			return _cursor.getBlob(index);
		else
			return null;
	}

	protected Float getDataFloat(String columnName) {
		if (_cursor != null && !_cursor.isClosed()) {
			int index = _cursor.getColumnIndex(columnName);
			if (index > -1)
				return _cursor.getFloat(index);
			else
				return null;
		} else
			return null;
	}

	protected Float getDataFloat(int index) {
		if (_cursor != null && !_cursor.isClosed())
			return _cursor.getFloat(index);
		else
			return null;
	}

	protected Long getDataLong(String columnName) {
		if (_cursor != null && !_cursor.isClosed()) {
			int index = _cursor.getColumnIndex(columnName);
			if (index > -1)
				return _cursor.getLong(index);
			else
				return null;
		} else
			return null;
	}

	protected Long getDataLong(int index) {
		if (_cursor != null && !_cursor.isClosed())
			return _cursor.getLong(index);
		else
			return null;
	}

	protected Short getDataShort(String columnName) {
		if (_cursor != null && !_cursor.isClosed()) {
			int index = _cursor.getColumnIndex(columnName);
			if (index > -1)
				return _cursor.getShort(index);
			else
				return null;
		} else
			return null;
	}

	protected Short getDataShort(int index) {
		if (_cursor != null && !_cursor.isClosed())
			return _cursor.getShort(index);
		else
			return null;
	}
	// end get data on cursor
}
